﻿package effects
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.external.ExternalInterface;
	import flash.geom.Matrix;
	import flash.net.URLRequest;
	public class Effect2 extends Effect
	{
		private var da:Number;
		private var w:int;
	 	private var h:int;	
	 	private var url:String = "assets/drawer.swf";
		private var loader:Loader = new Loader();
		private var d:*;
		private var bmp:BitmapData;
		private var matrix:Matrix ; 
		public function Effect2()
		{
			da=1;
			cacheAsBitmap=true;
			var request:URLRequest = new URLRequest(url);
			
			loader.load(request);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, draw);
			loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
		}
		public override function add():void
		{
			addEventListener(Event.ENTER_FRAME,render);
		}
		public override function remove():void
		{
			removeEventListener(Event.ENTER_FRAME,render);
		}
		private function draw(event:Event):void 
		{
			bmp = new BitmapData(Global._stage.stageWidth,Global._stage.stageHeight,true,0xFF000000);
			var bmpd:Bitmap = new Bitmap(bmp,"auto",true);
			addChild(bmpd);

			d=loader;
			d.x=(Global._stage.stageWidth/2)-200;
			d.y=(Global._stage.stageHeight/2)-100;

			addEventListener(Event.ENTER_FRAME,render);
		}
		private function ioErrorHandler(event:IOErrorEvent):void 
		{
			ExternalInterface.call("alert","Unable to load image: " + url);
		}
		private function render(e:Event):void
		{
			d.x += ((mouseX)-d.x)/24;
			d.y += (mouseY-d.y)/24;
				
			bmp.draw(d,d.transform.matrix);
		}
	}
}